package com.pinkpig.excel.ext.address;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author vincenfeng
 */
public class CellAddressReferenceStrategy implements CellWriteHandler {

    private List<String> toReferenceValues;

    private Map<String, List<Cell>> valueCellsMap = new HashMap<>();

    public CellAddressReferenceStrategy(List<String> toReferenceValues){
        this.toReferenceValues = toReferenceValues;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        if(toReferenceValues.contains(cell.getStringCellValue())){
            if(valueCellsMap.containsKey(cell.getStringCellValue())){
                valueCellsMap.get(cell.getStringCellValue()).add(cell);
                addCellReference(valueCellsMap.get(cell.getStringCellValue()));
            }else{
                List<Cell> cells = new ArrayList<>();
                cells.add(cell);
                valueCellsMap.put(cell.getStringCellValue(), cells);
            }
        }
    }

    private void addCellReference(List<Cell> cells){
        if(cells.size()<2){
            return;
        }
        Workbook workbook = cells.get(0).getSheet().getWorkbook();
        CreationHelper createHelper = workbook.getCreationHelper();
        CellStyle linkStyle0 = workbook.createCellStyle();
        CellStyle linkStyle1 = workbook.createCellStyle();
        Font linkFont = workbook.createFont();
        linkFont.setUnderline(Font.U_SINGLE);
        linkFont.setColor(IndexedColors.BLUE.getIndex());
        linkStyle0.setFont(linkFont);
        linkStyle1.setFont(linkFont);
        Hyperlink hyperlink0 = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
        hyperlink0.setAddress("#"+cells.get(0).getSheet().getSheetName()+"!"+cells.get(0).getAddress());
        Hyperlink hyperlink1 = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
        hyperlink1.setAddress("#"+cells.get(1).getSheet().getSheetName()+"!"+cells.get(1).getAddress());
        cells.get(0).setHyperlink(hyperlink1);
        copyStyle(cells.get(0).getCellStyle(), linkStyle0);
        cells.get(0).setCellStyle(linkStyle0);
        cells.get(1).setHyperlink(hyperlink0);
        copyStyle(cells.get(1).getCellStyle(), linkStyle1);
        cells.get(1).setCellStyle(linkStyle1);
    }

    private void copyStyle(CellStyle sourceCellStryle, CellStyle targetCellStryle){
        targetCellStryle.setBorderLeft(sourceCellStryle.getBorderLeftEnum());
        targetCellStryle.setBorderRight(sourceCellStryle.getBorderLeftEnum());
        targetCellStryle.setBorderTop(sourceCellStryle.getBorderLeftEnum());
        targetCellStryle.setBorderBottom(sourceCellStryle.getBorderLeftEnum());
        targetCellStryle.setTopBorderColor(sourceCellStryle.getTopBorderColor());
        targetCellStryle.setRightBorderColor(sourceCellStryle.getRightBorderColor());
        targetCellStryle.setBottomBorderColor(sourceCellStryle.getBottomBorderColor());
        targetCellStryle.setLeftBorderColor(sourceCellStryle.getLeftBorderColor());
        targetCellStryle.setFillPattern(sourceCellStryle.getFillPatternEnum());
        targetCellStryle.setFillForegroundColor(sourceCellStryle.getFillForegroundColor());
        targetCellStryle.setFillBackgroundColor(sourceCellStryle.getFillBackgroundColor());
    }
}
